package com.lwl.handler;

import com.lwl.utils.Constants;
import com.lwl.zk.ZookeeperUtils;
import org.I0Itec.zkclient.ZkClient;

public class ZookeeperHandler {
    //创建节点
    public static void createNode(String path) {

        //判断节点是否存在
        ZkClient session = ZookeeperUtils.getSession();
        boolean exists = session.exists(Constants.ZK_BASEPATH + path);
        if (exists) {
            System.out.println(path+"节点已存在zookeeper中，忽略不创建。");
            return;
        }
        //创建根节点
        boolean success = ZookeeperUtils.createNode(Constants.ZK_BASEPATH);

        if (success) {
            //创建临时节点，当节点断开后自动删除该节点
            session.createEphemeral(Constants.ZK_BASEPATH + path);
            System.out.println(path+"节点创建成功。");
        }
    }
}
